package pl.edu.uj.tcs.blef.protocol;

import java.io.PrintWriter;

/**
 * An abstract class which gives foundations of creating new requests.
 * @author Maciej Bendkowski
 *
 */
public abstract class Request {

	/**
	 * Contains the actual protocol message.
	 * @uml.property  name="message"
	 */
	protected String message;
	
	/**
	 * Reference to a proper handler, which handles the Request. 
	 * @uml.property  name="handler" readOnly="true"
	 */
	protected RequestHandler handler;

	/** 
     * Creates a new Request with the given protocol message.
	 */
	public Request(String message, RequestHandler handler){	
		this.message = message;
		this.handler = handler;
	}

	/**
	 * Sends the Request message via the given PrintWriter.
	 */
	public void send(PrintWriter printWriter){
		synchronized(printWriter){
			printWriter.println(message);	
			printWriter.flush();
		}
	}

	/**
	 * Getter of the property <tt>handler</tt>
	 * @return  Returns the handler.
	 * @uml.property  name="handler"
	 */
	public RequestHandler getHandler() {
		return handler;
	}

	/**
	 * Fills all argument-fields within the Request class based on the message arguments.
	 */
	protected abstract void fill();				
}
